<!--
允许用户从上传文件列表中选择要清空哪个文件或全部清空
-->
<template>
  <div>
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" class="colForm"
             @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="清空方式">
            <el-radio v-model="type" label="1">只清空指定文件上传内容</el-radio>
            <el-radio v-model="type" label="2">清空所有</el-radio>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" v-show="type==1">
        <el-col :span="24">
          <el-form-item label="要清空的文件">
            <el-select v-model="dataForm.fileName" style="width: 500px">
              <el-option v-for="item in files" :value="item" :key="item" :label="item"></el-option>
            </el-select>
          </el-form-item>

        </el-col>
      </el-row>
    </el-form>
    <span style="text-align: center;display:block;">
      <el-button @click="emitClose">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </div>
</template>

<script>
import tichuDetailController from "@/api/jiangjin/tichuDetailController";

export default {
  name: "tichuDetailRemoveSelect",
  data() {
    return {
      dataForm: {
        fileName: undefined,
      },
      type: '1',
      rootDeptCode: undefined,
      years: undefined,
      sourceType: undefined,
      files: []
    }
  },
  computed: {
    //默认校验为动态
    dataRule() {
      let a = {}
      if (this.type == 1) {
        a['fileName'] = [{
          required: true,
          message: '请选择要清空哪个上传文件关联的内容',
          trigger: ['blur', 'change']
        }]
      }
      return a
    }
  },
  methods: {
    init(years, sourceType, rootDeptCode) {
      this.years = years
      this.sourceType = sourceType
      this.rootDeptCode = rootDeptCode
      this.initSelect()
    },
    initSelect() {
      tichuDetailController.fetchAllFilenames(this.years, this.sourceType, this.rootDeptCode).then(({data}) => {
        this.files = data.data
      })
    },
    dataFormSubmit() {
      if (this.type == 2) {
        this.dataForm.fileName = ""
      }
      this.$refs['dataFormRef'].validate((valid) => {
        if (valid) {
          tichuDetailController.removeByFileName(this.years, this.sourceType, this.rootDeptCode, this.type, this.dataForm.fileName).then(({data}) => {
            this.$message.closeAll()
            this.$message.success('清空成功')
            this.$emit('refreshDataList')
            this.$emit("close")
          })
        }
      })
    },
    emitClose() {
      this.$emit("close")
    }
  }
}
</script>

<style scoped>

</style>
